package com.sl.discovery.modular.registry;

import java.net.URI;
import java.util.List;
import java.util.Map;

import cn.hutool.core.net.NetUtil;
import com.google.common.collect.Maps;
import jakarta.annotation.PostConstruct;

import org.springframework.beans.BeanUtils;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/**
 * @author xiaojing
 * @author changjin wei(魏昌进)
 */
public class DbRegistration implements Registration {

    /**
     * The metadata key of management port.
     */
    public static final String MANAGEMENT_PORT = "management.port";

    /**
     * The metadata key of management context-path.
     */
    public static final String MANAGEMENT_CONTEXT_PATH = "management.context-path";

    /**
     * The metadata key of management address.
     */
    public static final String MANAGEMENT_ADDRESS = "management.address";

    /**
     * The metadata key of management endpoints web base path.
     */
    public static final String MANAGEMENT_ENDPOINT_BASE_PATH = "management.endpoints.web.base-path";


    private ApplicationContext context;

    private int port = -1;

    private Map<String, String> metadata;

    private InetUtils inetUtils;

    public DbRegistration(ApplicationContext context, InetUtils inetUtils) {
        this.context = context;
        this.inetUtils = inetUtils;
    }

    @PostConstruct
    public void init() {
        Map<String, String> metadata = Maps.newHashMap();
        Environment env = context.getEnvironment();

        String endpointBasePath = env.getProperty(MANAGEMENT_ENDPOINT_BASE_PATH);
        if (StringUtils.hasLength(endpointBasePath)) {
            metadata.put(MANAGEMENT_ENDPOINT_BASE_PATH, endpointBasePath);
        }

        Integer managementPort = ManagementServerPortUtils.getPort(context);
        if (null != managementPort) {
            metadata.put(MANAGEMENT_PORT, managementPort.toString());
            String contextPath = env
                    .getProperty("management.server.servlet.context-path");
            String address = env.getProperty("management.server.address");
            if (StringUtils.hasLength(contextPath)) {
                metadata.put(MANAGEMENT_CONTEXT_PATH, contextPath);
            }
            if (StringUtils.hasLength(address)) {
                metadata.put(MANAGEMENT_ADDRESS, address);
            }
        }
        this.metadata = metadata;
    }


    @Override
    public String getInstanceId() {
        return getServiceId() + "_" + getHost();
    }

    @Override
    public String getServiceId() {
        return context.getId();
    }

    @Override
    public String getHost() {
        return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
    }

    @Override
    public int getPort() {
        return this.port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public boolean isSecure() {
        return true;
    }

    @Override
    public URI getUri() {
        return DefaultServiceInstance.getUri(this);
    }

    @Override
    public Map<String, String> getMetadata() {
        return metadata;
    }


}
